//
//  WBWelcomView.swift
//  ZWWEIBO
//
//  Created by xzw on 2018/4/8.
//  Copyright © 2018年 xzw. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomView: UIView {

    @IBOutlet weak var userImageView: UIImageView!
    
    @IBOutlet weak var tipLabel: UILabel!
    
    //用户图标距离底部距离
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    @IBOutlet weak var imageViewWidth: NSLayoutConstraint!
    
    //设置UI
    override func awakeFromNib() {
        guard let urlStr = WBNetworkManager.sharedInstance.userModel.avatar_large,
              let url = URL(string: urlStr) else{
            return
        }
        
        //如果不指定占位图片，之前设置的图片会被清空
        userImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))

        //设置圆角
        userImageView.layer.cornerRadius = imageViewWidth.constant * 0.5
        userImageView.layer.masksToBounds = true
        
        
    }
    
    
    class func loadWelcomeView() -> WBWelcomView{
        
        let nib = UINib(nibName: "WBWelcomView", bundle: nil)
        let view = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomView
    
        //设置满屏加载,默认600*600
        view.frame = UIScreen.main.bounds
        
        return view
    }
    
    //动画
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
       // self.layoutIfNeeded() //执行完之后，控件的位置就是xib布局的位置
    
         self.bottomCons.constant = self.bounds.height - 200
        
        //如果控件的frame还没计算好，所有约束会一起动画
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            
           //跟新约束
            self.layoutIfNeeded()
            
        }) { (_) in

            UIView.animate(withDuration: 1.0, animations: {
                self.tipLabel.alpha = 1.0
            }, completion: { (_) in
                
                self.removeFromSuperview() //移除视图
            })
            
        }

    }

}
